package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.course.entity.AdminCourseChapter;
import com.ksd.modules.course.mapper.AdminCourseChapterMapper;
import com.ksd.modules.course.po.AdminCourseChapterSavePO;
import com.ksd.modules.course.service.IAdminCourseChapterService;
import com.ksd.modules.course.service.IAdminCourseLessonService;
import com.ksd.modules.course.vo.AdminCourseChapterVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class AdminCourseChapterServiceImpl extends BaseServiceImpl<AdminCourseChapterMapper, AdminCourseChapter> implements IAdminCourseChapterService {



    @Autowired
    private IAdminCourseLessonService adminCourseLessonService;

    /**
     * 查询列表
     * @param courseId
     * @return
     */
    public List<AdminCourseChapterVo> findAdminCourseChapters(Long courseId){
        LambdaQueryWrapper<AdminCourseChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseChapter::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminCourseChapter::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminCourseChapter::getCourseId,courseId);
        lambdaQueryWrapper.orderByAsc(AdminCourseChapter::getSorted);
        List<AdminCourseChapterVo> adminCourseChapterVos = this.tranferListBo(this.list(lambdaQueryWrapper), AdminCourseChapterVo.class);
        if(CollectionUtils.isNotEmpty(adminCourseChapterVos)){
            for (AdminCourseChapterVo adminCourseChapterVo : adminCourseChapterVos) {
                adminCourseChapterVo.setChildren(adminCourseLessonService.findAdminCourseLessons(courseId,adminCourseChapterVo.getId()));
            }
        }
        return adminCourseChapterVos;
    }

    /**
     * 保存和更新
     * @param adminCourseChapterSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public boolean saveUpdateAdminCourseChapter(AdminCourseChapterSavePO adminCourseChapterSavePO){
        AdminCourseChapter adminCourseChapter = tranferBo(adminCourseChapterSavePO, AdminCourseChapter.class);
        adminCourseChapter.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminCourseChapter.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminCourseChapter);
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminCourseChapterVo getAdminCourseChapterById(Long id){
        AdminCourseChapter adminCourse = this.getById(id);
        AdminCourseChapterVo adminCourseVo = tranferBo(adminCourse, AdminCourseChapterVo.class);
        return adminCourseVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminCourseChapterById(Long id){
        boolean b = this.removeById(id);
        if(b){
            adminCourseLessonService.removeByChapterId(id);
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 根据课程删除节
     * @param courseId
     * @return
     */
    @Override
    public boolean removeByCourseId(Long courseId){
        LambdaQueryWrapper<AdminCourseChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseChapter::getCourseId,courseId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据课程删除节
     * @param courseIds
     * @return
     */
    @Override
    public boolean removeByCourseIds(List<Long> courseIds){
        LambdaQueryWrapper<AdminCourseChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdminCourseChapter::getCourseId,courseIds);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminCourseChapter data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
